package com.vic.delayedqueue.controller;

import com.vic.delayedqueue.config.DelayedQueue3Config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 基于插件的延时队列
 */
@Slf4j
@RestController
@RequestMapping("producer3")
public class Producer3Controller {

    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 消息的过期时间由参数传入
     * @param message
     * @param delayTime
     * @return
     */
    @GetMapping("/sendMsg/{message}/{delayTime}")
    public String sendExpirationMsg(@PathVariable String message, @PathVariable Integer delayTime) {
        log.info("当前时间: {},发送一条延时 {} 毫秒的信息给延时队列: {}", new Date(), delayTime, message);
        // 设置延时时间
        rabbitTemplate.convertAndSend(DelayedQueue3Config.DELAYED_EXCHANGE, DelayedQueue3Config.DELAYED_ROUTING_KEY, message, messagePostProcessor -> {
            messagePostProcessor.getMessageProperties().setDelay(delayTime);
            return messagePostProcessor;
        });
        return "ok";
    }

}
